package com.xiaren.comm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBHelp {
	public static String className_SQLSERVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static String url_SQLSERVER = "jdbc:sqlserver://192.168.1.102:1433;databaseName=JXD_Door";
	
	
	private String className = "oracle.jdbc.driver.OracleDriver";
	private String url = "jdbc:oracle:thin:@192.168.1.102:1521:ORCL";
	private String username = "neononecard";
	private String password = "neononecard";

	public Connection getConnection() {
		Connection con = null;
		// Context ctx;
		try {
			// ctx = new InitialContext();
			// DataSource ds =
			// (DataSource)ctx.lookup("java:comp/env/jdbc/booksSystem");
			// con = ds.getConnection();
			Class.forName(className);
			con = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	public Connection getConnection(String className, String url,
			String username, String password) {
		if (className != null) {
			this.className = className;
		}
		if (url != null) {
			this.url = url;
		}
		if (username != null) {
			this.username = username;
		}
		if (password != null) {
			this.password = password;
		}
		Connection con = null;
		try {
			Class.forName(this.className);
			con = DriverManager.getConnection(this.url, this.username,
					this.password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	public static void close(ResultSet rs, Statement st, PreparedStatement ps,
			Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
